package com.spzx.payment.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.payment.configure.AlipayConfig;
import com.spzx.payment.service.IAlipayService;
import com.spzx.payment.service.IPaymentInfoService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Author GQS @Date 2024/10/19/16:54 @Description
 */
@Slf4j
@Controller
@RequestMapping("/alipay")
public class AlipayController extends BaseController {

  @Autowired private IAlipayService alipayService;

  @Autowired private IPaymentInfoService paymentInfoService;

  @Operation(summary = "支付宝下单")
  @RequiresLogin
  @RequestMapping("submitAlipay/{orderNo}")
  @ResponseBody
  public AjaxResult submitAlipay(@PathVariable(value = "orderNo") String orderNo) {
    String form = alipayService.submitAlipay(orderNo);
    return success(form);
  }

  /**
   * 支付宝同步回调
   * @return
   */
  @RequestMapping("callback/return")
  public String callBack() {
    // 同步回调给用户展示信息
    return "redirect:" + AlipayConfig.return_order_url;
  }

  /**
   * 支付宝异步回调
   * @return
   */
  @RequestMapping("callback/notify")
  @ResponseBody
  public String alipayNotify(@RequestParam Map<String, String> paramMap, HttpServletRequest request) {
    // 支付宝异步回调
    return alipayService.alipayNotify(paramMap);

  }
}
